package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dto.SetmealDto;
import com.pojo.Category;
import com.pojo.Setmeal;
import com.pojo.SetmealDish;
import com.service.CategoryService;
import com.service.SetmealDishService;
import com.service.SetmealService;
import com.util.R;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;


    @Autowired
    private SetmealDishService setmealDishService;

    @PostMapping
    public R<String> add(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功 ");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        Page<Setmeal> page1 = new Page<>(page, pageSize);
        Page<SetmealDto> page2 = new Page<>();
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Strings.isNotEmpty(name),Setmeal::getName,name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(page1,wrapper);
        BeanUtils.copyProperties(page1,page2);
        List<Setmeal> list = page1.getRecords();
        List<SetmealDto> list1=list.stream().map((item)->{
            SetmealDto dto = new SetmealDto();
            BeanUtils.copyProperties(item,dto);
            Long id = item.getCategoryId();
            Category byId = categoryService.getById(id);
            String s = byId.getName();
            dto.setCategoryName(s);
            return dto;
        }).collect(Collectors.toList());
        page2.setRecords(list1);
        return R.success(page2);
    }
    @DeleteMapping
    public R<String> delete(@RequestParam(value = "ids") List<Long> ids){
        setmealService.removeBatchByIds(ids);
        return R.success("删除成功");
    }

    @PostMapping("/status/{sta}")
    public R<String> status(@PathVariable int sta,@RequestParam(value = "ids") List<Long> ids){
        Setmeal setmeal = new Setmeal();
        for (Long dishId : ids) {
            setmeal.setId(dishId);
            setmeal.setStatus(sta);
            setmealService.updateById(setmeal);
        }
        return R.success("修改成功");
    }

    @GetMapping("/{id}")
    public R<Setmeal> getByid(@PathVariable Long id){
        Setmeal byId = setmealService.getById(id);
        return R.success(byId);
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Long categoryId,int status){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId);
        wrapper.eq(Setmeal::getStatus,status);
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(wrapper);
        return R.success(list);
    }

    @GetMapping("/dish/{id}")
    public R<SetmealDto> getDish(@PathVariable Long id){
        Setmeal byId = setmealService.getById(id);
        SetmealDto dto = new SetmealDto();
        BeanUtils.copyProperties(byId,dto);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        dto.setSetmealDishes(list);
        return R.success(dto);
    }
}
